//
//  HMVisitorView.swift
//  微博
//
//  Created by 程双岐 on 16/9/20.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit

protocol visitorViewDelegate : NSObjectProtocol {
    func visitorViewLoginActon()
}

class HMVisitorView: UIView {
    
    var block :(()->())?
    
    var visitorDelegate : visitorViewDelegate?

    
    private lazy var cycleImageView : UIImageView = UIImageView(image: UIImage(named : "visitordiscover_feed_image_smallicon"))
    
    private lazy var maskImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var iconImageVieww : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var messageLabel : UILabel = {
    
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        label.textAlignment = .center
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var loginButton : UIButton = {
    let button = UIButton()
        button.setBackgroundImage(UIImage(named : "common_button_white_disable"), for: .normal)
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.addTarget(self, action: #selector(pushAction), for: .touchUpInside)
        button.adjustsImageWhenHighlighted = false
        return button
    
    }()
    private lazy var registerButton : UIButton = {
       let button = UIButton()
        button.setBackgroundImage(UIImage(named : "common_button_white_disable"), for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.setTitle("注册", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.addTarget(self , action: #selector(pushAction), for: .touchUpInside)
        button.adjustsImageWhenHighlighted = false
        return button
        
    }()
    
    
    
   override init(frame: CGRect) {
        super.init(frame: frame)

        setupUI()
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI (){
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageVieww)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        cycleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        iconImageVieww.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconImageVieww)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.width.equalTo(224)
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        registerButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(loginButton)
            make.size.equalTo(loginButton)
        }
        
    }
    
    
    func updateAisitorView (message : String?,imageName : String?){
        if let mess = message,let name = imageName {
            
            messageLabel.text = mess
            iconImageVieww.image = UIImage(named: name)
            cycleImageView.isHidden = true
            
        }else{
          
            startAnimation()

        }
    }
    
    
    func pushAction (){
        block?()
        visitorDelegate?.visitorViewLoginActon()
    }
    
    private func startAnimation(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = M_PI * 2
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.isRemovedOnCompletion = false
        cycleImageView.layer.add(animation, forKey: nil)
        
        
    }
    
    
}
